package com.gnerv.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.*;
import com.gnerv.cms.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 基础平台-角色 controller 实现类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "角色管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.saveRole(roleSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.createRole(roleSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.GET)
    public Result deleteRole(@PathVariable("roleId") String roleId) {
        roleService.deleteRole(roleId);
        return new Result();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.updateRole(roleSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/{roleId}/status/{status}", method = RequestMethod.GET)
    public Result updateRoleStatus(@PathVariable("roleId") String roleId, @PathVariable("status") Integer status) {
        roleService.updateRoleStatus(roleId, status);
        return new Result();
    }

    @RequestMapping(value = "/select/{roleId}", method = RequestMethod.GET)
    public Result selectRole(@PathVariable("roleId") String roleId) {
        RoleDTO role = roleService.selectRole(roleId);
        return new Result().data("role", role);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listRole(@RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "300") Integer size,
                           RoleSearchDTO roleSearchDTO) {
        Page<RoleDTO> roles = roleService.listRole(current, size, roleSearchDTO);
        return new Result().data("roles", roles);
    }

    @RequestMapping(value = "/bind/menu", method = RequestMethod.POST)
    public Result bindRoleMenu(@RequestBody RoleMenuDTO roleMenuDTO) {
        roleService.bindRoleMenu(roleMenuDTO.getRoleId(), roleMenuDTO.getMenuIds());
        return new Result();
    }

    @RequestMapping(value = "/bind/user", method = RequestMethod.POST)
    public Result bindRoleUser(@RequestBody RoleUserDTO roleUserDTO) {
        roleService.bindRoleUser(roleUserDTO.getRoleId(), roleUserDTO.getUserIds());
        return new Result();
    }

}
